package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import db.oracle.OracleConnMgr;
import dto.C2DTO;
import dto.MBoardDTO;
import dto.MBoard_repDTO;

public class MBoardDAO {
	public Vector getMBoardList(){
		Vector v1 = new Vector();
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String insertQuery = "select * from t_usedMarket";
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				MBoardDTO dto = new MBoardDTO();
				dto.setMboardSeq(rs.getInt("f_usedMarket_seq"));
				dto.setMboardMemId(rs.getString("f_member_id"));
				dto.setMboardTitle(rs.getString("f_usedMarket_title"));
				dto.setMboardImagePath(rs.getString("f_usedMarket_image"));
				dto.setMboardContent(rs.getString("f_usedMarket_contents"));
				dto.setMboardRead(rs.getInt("f_usedMarket_read"));
				dto.setMboardDate(rs.getString("f_usedMarket_date"));
				dto.setMboardSold(rs.getString("f_usedMarket_sold"));
				
				v1.addElement(dto);
			}		
			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return v1;
		
	}
	public int createMBoard(MBoardDTO dto){
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		String insertQuery = "insert into t_usedMarket" +
				"(f_usedMarket_seq, f_member_id, f_usedMarket_title, " +
				"f_usedMarket_image, f_usedMarket_contents, " +
				"f_usedMarket_sold, f_usedMarket_date, f_usedMarket_read) " +
				"values(MEMBER_SEQ.nextval,?,?,?,?,?,SYSTIMESTAMP,0)";
		int ret = 0;
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			pstmt.setString(1, dto.getMboardMemId());
			pstmt.setString(2, dto.getMboardTitle());
			pstmt.setString(3, dto.getMboardImagePath());
			pstmt.setString(4, dto.getMboardContent());
			pstmt.setString(5, dto.getMboardSold());
			
			ret = pstmt.executeUpdate();			
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return ret;
	}
	public MBoardDTO readMboard(int seq){
		MBoardDTO dto = new MBoardDTO();
		OracleConnMgr mgr = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String insertQuery = 
			"select * from t_usedMarket where f_usedMarket_seq = ?";
		
		try {
			mgr = OracleConnMgr.getInstance();
			conn = mgr.getConnection();
			pstmt = conn.prepareStatement(insertQuery);
			pstmt.setInt(1, seq);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				dto.setMboardSeq(rs.getInt("f_usedMarket_seq"));
				dto.setMboardTitle(rs.getString("f_usedMarket_title"));
				dto.setMboardMemId(rs.getString("f_member_id"));
				dto.setMboardImagePath(rs.getString("f_usedMarket_image"));
				dto.setMboardContent(rs.getString("f_usedMarket_contents"));
				dto.setMboardDate(rs.getString("f_usedMarket_date"));
				dto.setMboardRead(rs.getInt("f_usedMarket_read"));
				dto.setMboardSold(rs.getString("f_usedMarket_sold"));
			}
		} catch (SQLException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			try {
				mgr.close(conn, pstmt, rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return dto;
	}
	public int updateMboard(MBoardDTO dto){
		return 0;
	}
	public int deleteMboard(int seq){
		return 0;
	}
	public Vector getMboardRlyList(int mboardseq){
		return null;
	}
	public int createMBoardRly(MBoard_repDTO dto){
		return 0;
	}
	public int updateMboardRly(MBoard_repDTO dto){
		return 0;
	}
	public int deleteMboardRly(int seq){
		return 0;
	}
}
